import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.rhb.rhbaiagent.demo.invoke.TestApiKey;

import java.util.HashMap;
import java.util.Map;

public class HttpAIInvoke {
    public static void main(String[] args) {
        // 定义请求的 URL
        String url = "https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation";

        // 设置请求头
        Map<String, String> headers = new HashMap<>();
        headers.put("Authorization", "Bearer " + TestApiKey.API_KEY);
        headers.put("Content-Type", "application/json");

        // 定义请求体
        String jsonInputString = "{" +
                "\"model\": \"qwen-plus\"," +
                "\"input\": {" +
                "\"messages\": [" +
                "{" +
                "\"role\": \"system\"," +
                "\"content\": \"You are a helpful assistant.\"" +
                "}," +
                "{" +
                "\"role\": \"user\"," +
                "\"content\": \"你是谁？\"" +
                "}" +
                "]" +
                "}," +
                "\"parameters\": {" +
                "\"result_format\": \"message\"" +
                "}" +
                "}";

        // 发送 POST 请求
        HttpResponse response = HttpRequest.post(url)
                .addHeaders(headers)
                .body(jsonInputString)
                .execute();

        // 获取响应码
        int status = response.getStatus();
        System.out.println("Response Code: " + status);

        // 获取响应内容
        String body = response.body();
        System.out.println("Response: " + body);
    }
}    